<?php
/**
 * Powered By ThinkPHP 5.0.12
 * Copyright (c) 2017 http://www.bojuwang.net All rights reserved.
 * Created by PhpStorm.
 * Date: 2018/4/23 0023
 * Time: 上午 11:37
 * Author: NanTao <260053530@qq.com>
 */
namespace app\index\controller;
use think\Db;
class Goods extends Common
{
    public function index()
    {
        $where = ['status'=>0];
        $sort = input('param.sort');
        $cateId = input('param.cateId');
        if (isset($sort))
        {
            if ($sort == 'default')
            {
                $order = 'add_time desc';
            } elseif ($sort == 'price') {
                $order = 'sell_price asc';
            } elseif ($sort == 'pricedesc') {
                $order = 'sell_price desc';
            }
            $this->assign('sort',$sort);
        }
        if (isset($cateId))
        {
            $where['category_id']=$cateId;
            $this->assign('cateId',$cateId);
        }
        $list = Db::name('goods')->where($where)->order($order)->limit('add_time desc')->select();
        $this->assign('list',$list);
        $this->assign('list',$list);
        $category = Db::name('sys_category')->where('channel_id',2)->select();
        $this->assign('category',$category);
        return $this->fetch();
    }
    public function show()
    {
        $id = input('param.id');
        $info = Db::name('goods')->where('id',$id)->find();
        $info['collection'] = goods_is_collection(session('userId'),$id);
        $this->assign('info',$info);
        $imglist = Db::name('img')->where(['type'=>1,'main_id'=>$id])->select();
        $this->assign('imglist',$imglist);
        $cartCount = get_cart_count(session('userId'));
        $this->assign('cartCount',$cartCount);



        $savepath = 'public/upload/usergoodsqcode';//存放在当前目录的upload文件夹下
        $savename = get_user_info_data(session('userId'),'user_name').'_'.$id.".jpg";
        $path = $savepath.'/'.$savename;
        $url = sysconf('website')."/index/goods/show/id/".$id."/parentId/".session('userId');
        $qrcode = $this->createQrcode($url,$path);


        $this->assign('qrcode',$qrcode);
        $this->assign('title',$info['title']);
        $this->assign('icon',$info['img_url']);
        $user = get_user_model(session('userId'));
        $this->assign('username',$user['user_nickname']);
        return $this->fetch();
    }
    public function goodlist()
    {
        $category_id = input('param.category_id');
        $cate_list = Db::name('sys_category')->where('channel_id',2)->select();
        $category_id=$category_id?$category_id:$cate_list[0]['id'];
        $goods_list = Db::name('goods')->where('category_id',$category_id)->select();
        $this->assign('cate_list',$cate_list);
        $this->assign('goods_list',$goods_list);
        return $this->fetch();
    }
    public function getImage($url){
        $ch = curl_init($url);
        curl_setopt($ch, CURLOPT_HEADER,0);
        curl_setopt($ch, CURLOPT_NOBODY,0);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST,FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        $package = curl_exec($ch);
        $httpinfo = curl_getinfo($ch);
        curl_close($ch);
        return array_merge(array('body'=>$package),array('header'=>$httpinfo));
    }




    /*
     * 商品收藏与取消收藏
     * */
    public function collGoods()
    {
        $userId = session('userId');
        $goodsId = input('param.goodsId');
        $action = input('param.action');
        switch ($action) {
            case 'coll':
                $data = ['user_id'=>$userId,'goods_id'=>$goodsId];
                if (Db::name('goods_collection')->where($data)->find())
                {
                    json_msg(0,'已收藏');
                } else {
                    if (Db::name('goods_collection')->insert($data))
                    {
                        json_msg(0,'已收藏');
                    } else {
                        json_msg(1,'收藏失败');
                    }
                }
                break;
            case 'cancel':
                $data = ['user_id'=>$userId,'goods_id'=>$goodsId];
                if (Db::name('goods_collection')->where($data)->find())
                {
                    if (Db::name('goods_collection')->where($data)->delete())
                    {
                        json_msg(0,'已取消');
                    } else {
                        json_msg(1,'取消失败');
                    }
                } else {
                    json_msg(0,'已取消');
                }
                break;
        }
    }



 
}